package com.itheima.health.mq;

import com.itheima.health.service.OrderService;
import com.rabbitmq.client.Channel;

import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author dejawu
 * @Title: DeleteOrderMQ
 * @Description: TODO
 * @date 2020/12/23
 */
@Component
@Log4j2
public class DeleteOrderMQ {

    @Reference
    OrderService orderService;

    @RabbitListener(queues = "heimadelay_queue")
    public void recover(String msg, Message message, Channel channel){
        log.info("接收时间:{},接受内容:{}", LocalDateTime.now(), msg);
        //通知 MQ 消息已被接收,可以ACK(从队列中删除)了
        try {
            orderService.removeById(Long.parseLong(msg));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            log.error("消息消费失败！错误信息：{}",e.getMessage());
        }

    }
}
